package com.ruoyi.web.controller.market.form;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.FormInfo;
import com.ruoyi.db.domain.FormInfoHis;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.db.model.param.FormInfoHisParam;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.form.service.IFormInfoHisService;
import com.ruoyi.form.service.IFormInfoService;
import com.ruoyi.web.wapper.FormInfoHisWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 表单推送历史Controller
 * 
 * @author ruoyi
 * @date 2022-08-16
 */
@Controller
@RequestMapping("/form/his")
public class FormInfoHisController extends BaseController
{
    private String prefix = "form/his";

    @Autowired
    private IFormInfoHisService formInfoHisService;
    @Autowired
    private IFormInfoService formInfoService;
    @Autowired
    private IChannelInfoService channelInfoService;

    @RequiresPermissions("form:his:view")
    @GetMapping()
    public String his(ModelMap modelMap)
    {

        FormInfo formInfo = new FormInfo();
        List<Map<String, Object>> formInfoList = formInfoService.selectFormInfoListMap(formInfo);
        modelMap.put("formInfoList", formInfoList);

        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        List<Map<String, String>> channelInfoList = channelInfoService.selectSimpleMapInfo(channelInfoParam);
        modelMap.put("channelInfoList", channelInfoList);

        return prefix + "/his";
    }

    /**
     * 查询表单推送历史列表
     */
    @RequiresPermissions("form:his:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormInfoHisParam formInfoHisParam)
    {
        startPage();
        List<Map<String, Object>> mapList = formInfoHisService.selectFormInfoHisListMap(formInfoHisParam);
        mapList = new FormInfoHisWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出表单推送历史列表
     */
    @RequiresPermissions("form:his:export")
    @Log(title = "表单推送历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FormInfoHis formInfoHis)
    {
        List<FormInfoHis> list = formInfoHisService.selectFormInfoHisList(formInfoHis);
        ExcelUtil<FormInfoHis> util = new ExcelUtil<FormInfoHis>(FormInfoHis.class);
        return util.exportExcel(list, "表单推送历史数据");
    }

    /**
     * 新增表单推送历史
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存表单推送历史
     */
    @RequiresPermissions("form:his:add")
    @Log(title = "表单推送历史", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormInfoHis formInfoHis)
    {
        return toAjax(formInfoHisService.insertFormInfoHis(formInfoHis));
    }

    /**
     * 修改表单推送历史
     */
    @RequiresPermissions("form:his:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        FormInfoHis formInfoHis = formInfoHisService.selectFormInfoHisById(id);
        mmap.put("formInfoHis", formInfoHis);
        return prefix + "/edit";
    }

    /**
     * 修改保存表单推送历史
     */
    @RequiresPermissions("form:his:edit")
    @Log(title = "表单推送历史", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormInfoHis formInfoHis)
    {
        return toAjax(formInfoHisService.updateFormInfoHis(formInfoHis));
    }

    /**
     * 修改表单推送历史
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        FormInfoHis formInfoHis = formInfoHisService.selectFormInfoHisById(id);
        mmap.put("formInfoHis", formInfoHis);
        return prefix + "/detail";
    }

    /**
     * 删除表单推送历史
     */
    @RequiresPermissions("form:his:remove")
    @Log(title = "表单推送历史", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(formInfoHisService.deleteFormInfoHisByIds(ids));
    }
}
